1 package org.apache.maven.surefire.junitcore;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Map;
23 import org.apache.maven.surefire.report.ConsoleLogger;
24 import org.apache.maven.surefire.report.ConsoleOutputReceiver;
25 import org.apache.maven.surefire.report.ReportEntry;
26 import org.apache.maven.surefire.report.ReporterFactory;
27 import org.apache.maven.surefire.report.RunListener;
28 import org.apache.maven.surefire.testset.TestSetFailedException;
29
30
31
32
33
34
35
36
37
38
39
40
41 public abstract class ConcurrentRunListener
42 implements RunListener, ConsoleOutputReceiver
43 {
44 private final Map<String, TestSet> classMethodCounts;
45
46 private final ThreadLocal<RunListener> reporterManagerThreadLocal;
47
48 private final boolean reportImmediately;
49
50 private final ReporterFactory reporterFactory;
51
52 private final ConsoleLogger consoleLogger;
53
54 ConcurrentRunListener( ReporterFactory reporterFactory, ConsoleLogger consoleLogger, boolean reportImmediately,
55 Map<String, TestSet> classMethodCounts )
56 throws TestSetFailedException
57 {
58 this.reportImmediately = reportImmediately;
59 this.reporterFactory = reporterFactory;
60 this.classMethodCounts = classMethodCounts;
61 this.consoleLogger = consoleLogger;
62
63 this.reporterManagerThreadLocal = new ThreadLocal<RunListener>()
64 {
65 @Override
66 protected RunListener initialValue()
67 {
68 return ConcurrentRunListener.this.reporterFactory.createReporter();
69 }
70 };
71 }
72
73 public void testSetStarting( ReportEntry description )
74 {
75 }
76
77 public void testSetCompleted( ReportEntry result )
78 {
79 final RunListener reporterManager = getRunListener();
80 for ( TestSet testSet : classMethodCounts.values() )
81 {
82 testSet.replay( reporterManager );
83 }
84 reporterManagerThreadLocal.remove();
85 }
86
87 public void testFailed( ReportEntry failure )
88 {
89 final TestMethod testMethod = getOrCreateThreadAttachedTestMethod( failure );
90 if ( testMethod != null )
91 {
92 testMethod.testFailure( failure );
93 testMethod.detachFromCurrentThread();
94 }
95 }
96
97 public void testError( ReportEntry failure )
98 {
99 final TestMethod testMethod = getOrCreateThreadAttachedTestMethod( failure );
100 if ( testMethod != null )
101 {
102 testMethod.testError( failure );
103 testMethod.detachFromCurrentThread();
104 }
105 }
106
107 public void testSkipped( ReportEntry description )
108 {
109 TestSet testSet = getTestSet( description );
110 TestMethod testMethod = testSet.createThreadAttachedTestMethod( description );
111 testMethod.testIgnored( description );
112 testSet.incrementFinishedTests( getRunListener(), reportImmediately );
113 testMethod.detachFromCurrentThread();
114 }
115
116 public void testExecutionSkippedByUser()
117 {
118
119 reporterManagerThreadLocal.get().testExecutionSkippedByUser();
120 }
121
122 public void testAssumptionFailure( ReportEntry failure )
123 {
124 final TestMethod testMethod = getOrCreateThreadAttachedTestMethod( failure );
125 if ( testMethod != null )
126 {
127 testMethod.testIgnored( failure );
128 testMethod.detachFromCurrentThread();
129 }
130 }
131
132 public void testStarting( ReportEntry description )
133 {
134 TestSet testSet = getTestSet( description );
135 testSet.createThreadAttachedTestMethod( description );
136
137 checkIfTestSetCanBeReported( testSet );
138 testSet.attachToThread();
139 }
140
141 public void testSucceeded( ReportEntry report )
142 {
143 TestMethod testMethod = getTestMethod();
144 if ( null != testMethod )
145 {
146 testMethod.testFinished();
147 testMethod.getTestSet().incrementFinishedTests( getRunListener(), reportImmediately );
148 testMethod.detachFromCurrentThread();
149 }
150 }
151
152 private TestMethod getOrCreateThreadAttachedTestMethod( ReportEntry description )
153 {
154 TestMethod threadTestMethod = TestMethod.getThreadTestMethod();
155 if ( threadTestMethod != null )
156 {
157 return threadTestMethod;
158 }
159 TestSet testSet = getTestSet( description );
160 if ( testSet == null )
161 {
162 consoleLogger.info( description.getName() );
163 consoleLogger.info( description.getStackTraceWriter().writeTraceToString() );
164 return null;
165 }
166 else
167 {
168 return testSet.createThreadAttachedTestMethod( description );
169 }
170 }
171
172 protected abstract void checkIfTestSetCanBeReported( TestSet testSetForTest );
173
174 TestMethod getTestMethod()
175 {
176 return TestMethod.getThreadTestMethod();
177 }
178
179 TestSet getTestSet( ReportEntry description )
180 {
181 return classMethodCounts.get( description.getSourceName() );
182 }
183
184 RunListener getRunListener()
185 {
186 return reporterManagerThreadLocal.get();
187 }
188
189 public static ConcurrentRunListener createInstance( Map<String, TestSet> classMethodCounts,
190 ReporterFactory reporterFactory,
191 boolean parallelClasses, boolean parallelBoth,
192 ConsoleLogger consoleLogger )
193 throws TestSetFailedException
194 {
195 return parallelClasses
196 ? new ClassesParallelRunListener( classMethodCounts, reporterFactory, consoleLogger )
197 : new MethodsParallelRunListener( classMethodCounts, reporterFactory, !parallelBoth, consoleLogger );
198 }
199
200
201 public void writeTestOutput( byte[] buf, int off, int len, boolean stdout )
202 {
203 TestMethod threadTestMethod = TestMethod.getThreadTestMethod();
204 if ( threadTestMethod != null )
205 {
206 final LogicalStream logicalStream = threadTestMethod.getLogicalStream();
207 logicalStream.write( stdout, buf, off, len );
208 }
209 else
210 {
211
212 consoleLogger.info( new String( buf, off, len ) );
213 }
214 }
215 }